﻿// -----------------------------------------------------------------------
// <copyright file="VsHierarchyItem.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Rosieks.VisualStudio.Web.Flow.CustomCode
{
    using Microsoft.VisualStudio.Shell.Interop;
    using Rosieks.VisualStudio.Web.Flow.Interop;
    using OLE = Microsoft.VisualStudio.OLE.Interop;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    internal sealed class VsHierarchyItem
    {
        private uint pitemid;
        private IVsUIHierarchy ppUIH;

        internal VsHierarchyItem(uint id, IVsUIHierarchy hier)
        {
            this.pitemid = pitemid;
            this.ppUIH = ppUIH;
        }

        internal static VsHierarchyItem CreateFromMonikerOnly(string moniker)
        {
            VsHierarchyItem item = null;
            if (!string.IsNullOrEmpty(moniker))
            {
                IVsUIShellOpenDocument service = WebflowUtil.GetService<IVsUIShellOpenDocument>();
                if (service != null)
                {
                    int num;
                    OLE.IServiceProvider provider;
                    IVsUIHierarchy ppUIH = null;
                    uint pitemid = 0;
                    if (NativeMethods.Succeeded(service.IsDocumentInAProject(moniker, out ppUIH, out pitemid, out provider, out num)) && ppUIH != null && pitemid != 0)
                    {
                        item = new VsHierarchyItem(pitemid, ppUIH);
                    }
                }
            }

            return item;
        }
    }
}
